<?php

namespace App\Api;

use PhalApi\Api;


/**
 * 万国觉醒 存token
 *
 * @author: dogstar <chanzonghuang@gmail.com> 2014-10-04
 */
class Rok extends Api
{

    public function getRules()
    {
        return array(
            'token' => array(
                'token'    => array('name' => 'token', 'require' => 'true', 'desc' => 'token'),
                'cradid'   => array('name' => 'cradid', 'desc' => '身份证_id'),
                'phone'    => array('name' => 'phone', 'desc' => '手机号'),
                'city'     => array('name' => 'city', 'desc' => '主城等级'),
                'fighting' => array('name' => 'fighting', 'desc' => '战力'),
                'qu'       => array('name' => 'qu', 'desc' => '所在区服'),
                'idfa'     => array('name' => 'idfa', 'desc' => 'idfa'),
                'food'     => array('name' => 'food', 'desc' => '玉米'),
                'wood'     => array('name' => 'wood', 'desc' => '木材'),
                'stone'    => array('name' => 'stone', 'desc' => '石头'),
                'gold'     => array('name' => 'gold', 'desc' => '金币'),
                'red'      => array('name' => 'red', 'desc' => '红宝石'),
                'state'    => array('name' => 'state', 'desc' => '运行状态'),
                'note'     => array('name' => 'note', 'desc' => '备注'),
                'qq'       => array('name' => 'qq', 'desc' => 'qq号'),
                'user_id'       => array('name' => 'user_id', 'desc' => 'user_id'),
                'locals'       => array('name' => 'locals', 'desc' => 'locals'),
            ),
        );
    }

    /**
     * rok-token
     * @desc 多条件查询接口
     * @return array data 被查数据
     * @exception 400 非法请求，参数传递错误
     */
    public function token()
    {
        header('Access-Control-Allow-Origin:*');

        $token = $this->token;
        $tokeninfo = \PhalApi\DI()->notorm
            ->rok_account
            ->select('id', 'token')
            ->where('token', $token)
            ->fetchOne();

        $save = array();
        foreach ($this as $key => $v) {
            if ($v) {
                $save[$key] = $v;
            }
        }

        $save['time'] = date('Y-m-d H:i:s', time());

        if ($tokeninfo) {
            $tokeninfo = \PhalApi\DI()->notorm
                ->rok_account
                ->where('id', $tokeninfo['id'])
                ->update($save);
        } else {
            $save['addtime'] = date('Y-m-d H:i:s', time());
            $save['due_time'] = date('Y-m-d H:i:s', time()+30*24*60*60);
            $tokeninfo = \PhalApi\DI()->notorm
                ->rok_account
                ->insert($save);
        }

        return $tokeninfo;
    }


}
